package com.jianyun.jyzs.model;

import android.util.Log;

import com.google.gson.Gson;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.dao.EngineerDao;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.CustomCallback;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.model.imdoel.ICreateEngiModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangshijun on 2018/5/31.
 */

public class CreateEngineerModel implements ICreateEngiModel {

    private static CreateEngineerModel model;

    private CreateEngineerModel(){}

    public static CreateEngineerModel getInstance(){
        if (model==null){
            model=new CreateEngineerModel();
        }
        return model;
    }

    @Override
    public void getOneEngineer(final EngineerDao dao,
                               HashMap<String, String> map, final OnCreateEngineerListener onCreateEngineerListener) {

        Api api = RetrofitHelper.builderCeshiHttp().create(Api.class);
        api.getNewOneEngineer(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<EngineeringResult>() {
                    @Override
                    protected void onResponse(EngineeringResult response) {
                        Log.i("test",response.toString());
                        if (response.isResult()) {
                            Gson gson = new Gson();
                            List<EngineeringResult.EngineerBean> list = response.getList();
                            for (EngineeringResult.EngineerBean ee : list) {
                                ee.setListAgress(gson.toJson(ee.getAgress()));
                                ee.setListAttachment(gson.toJson(ee.getAttachment()));
                                ee.setListDisscuss(gson.toJson(ee.getDiscuss()));
                                List<EngineeringResult.EngineerBean.AttachmentBean> attachment = ee.getAttachment();
                                ArrayList<String> images = new ArrayList<>();
                                for (EngineeringResult.EngineerBean.AttachmentBean eea : attachment) {
                                    if (eea.getPhotoPath() != null) images.add(eea.getId());
                                }
                                ee.setListImages(gson.toJson(images));
                                ee.setDeleted(response.isDeleted());
                                dao.insert(ee);
                            }
                            onCreateEngineerListener.onCreateSuccess();
                        } else {
                            onCreateEngineerListener.onFailed(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                       onCreateEngineerListener.onFailed(msg);
                    }
                });
    }
}
